package com.yunfangdata.web.controller;


import com.yunfangdata.domain.Department;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.IDepartmentService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import com.yunfangdata.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequiresPermissions("department:index")
    @PermissionName("部门首页")
    @RequestMapping("")
    public String index(){
        return "department/list";
    }

    @RequiresPermissions("department:list")
    @PermissionName("部门列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return departmentService.query(qo);
    }

    @RequiresPermissions("department:save")
    @PermissionName("部门保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Department department){
        JsonResult result = new JsonResult();
        try {
            departmentService.save(department);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;

    }

    @RequiresPermissions("department:update")
    @PermissionName("部门更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Department department){
        JsonResult result = new JsonResult();
        try {
            departmentService.update(department);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;

    }

    @RequiresPermissions("department:changeState")
    @PermissionName("部门状态")
    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            departmentService.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;

    }


    //部门下拉框
    @RequestMapping("depts")
    @ResponseBody
    public List<Department> depts(){
        return departmentService.list();
    }


    //查询部门列表

    @RequestMapping("seleceAllDept")
    @ResponseBody
    public List<Department> seleceAllDept(){
        return departmentService.list();
    }



}
